<template>
  <div class="resume-container">
    <!-- 顶部导航栏 -->
    <header class="resume-header">
      <div class="header-content">
        <div class="home-icon" @click="goToHome" title="返回主页">
            <i class="fas fa-home"></i>
        </div>
        <h1 class="resume-title">个人简历</h1>
        <button class="download-btn" @click="downloadResume">
          <i class="fas fa-download"></i>
          <span>下载简历</span>
        </button>
      </div>
    </header>
    
    <!-- 简历主体内容 -->
    <main class="resume-main">
      <!-- 个人信息 -->
      <section class="profile-section">
        <div class="profile-image">
          <img :src="profile.avatar" alt="个人头像" class="avatar">
        </div>
        <div class="profile-info">
          <h2 class="name">{{ profile.name }}</h2>
          <p class="title">{{ profile.title }}</p>
          <div class="contact-info">
            <div v-for="(item, index) in profile.contacts" :key="index" class="contact-item">
              <i :class="item.icon"></i>
              <span>{{ item.value }}</span>
            </div>
          </div>
        </div>
      </section>
      
      <!-- 工作经历 -->
      <section class="section">
        <h2 class="section-title">
          <i class="fas fa-briefcase"></i>
          <span>工作经历</span>
        </h2>
        <div class="section-content">
          <div v-for="(exp, index) in experiences" :key="index" class="experience-item">
            <div class="timeline">
              <div class="timeline-dot"></div>
              <div v-if="index !== experiences.length - 1" class="timeline-line"></div>
            </div>
            <div class="experience-content">
              <h3 class="company">{{ exp.company }}</h3>
              <div class="meta">
                <span class="position">{{ exp.position }}</span>
                <span class="duration">{{ exp.startDate }} - {{ exp.endDate || '至今' }}</span>
                <span class="years" v-if="exp.years">· {{ exp.years }}</span>
              </div>
              <div class="description" v-html="exp.description"></div>
              <div class="tech-stack" v-if="exp.technologies">
                <span v-for="(tech, i) in exp.technologies" :key="i" class="tech-tag">{{ tech }}</span>
              </div>
            </div>
          </div>
        </div>
      </section>
      
      <!-- 教育背景 -->
      <section class="section">
        <h2 class="section-title">
          <i class="fas fa-graduation-cap"></i>
          <span>教育背景</span>
        </h2>
        <div class="section-content">
          <div v-for="(edu, index) in education" :key="index" class="education-item">
            <div class="timeline">
              <div class="timeline-dot"></div>
              <div v-if="index !== education.length - 1" class="timeline-line"></div>
            </div>
            <div class="education-content">
              <h3 class="school">{{ edu.school }}</h3>
              <div class="meta">
                <span class="degree">{{ edu.degree }}</span>
                <span class="major">{{ edu.major }}</span>
                <span class="duration">{{ edu.startDate }} - {{ edu.endDate }}</span>
              </div>
              <div class="description" v-if="edu.description" v-html="edu.description"></div>
            </div>
          </div>
        </div>
      </section>
      
      <!-- 技能专长 -->
      <section class="section">
        <h2 class="section-title">
          <i class="fas fa-code"></i>
          <span>技能专长</span>
        </h2>
        <div class="section-content skills">
          <div v-for="(skill, index) in skills" :key="index" class="skill-item">
            <h3 class="skill-category">{{ skill.category }}</h3>
            <div class="skill-list">
              <div v-for="(item, i) in skill.items" :key="i" class="skill">
                <span class="skill-name">{{ item.name }}</span>
                <div class="skill-bar">
                  <div class="skill-level" :style="{ width: item.level + '%' }"></div>
                </div>
                <span class="skill-percent">{{ item.level }}%</span>
              </div>
            </div>
          </div>
        </div>
      </section>
      
      <!-- 项目经验 -->
      <section class="section">
        <h2 class="section-title">
          <i class="fas fa-project-diagram"></i>
          <span>项目经验</span>
        </h2>
        <div class="section-content projects">
          <div v-for="(project, index) in projects" :key="index" class="project-item">
            <h3 class="project-title">{{ project.name }}</h3>
            <div class="project-meta">
              <span class="role">{{ project.role }}</span>
              <span class="duration">{{ project.duration }}</span>
            </div>
            <div class="project-description" v-html="project.description"></div>
            <div class="project-responsibilities" v-if="project.responsibilities">
              <h4>主要职责：</h4>
              <ul>
                <li v-for="(resp, i) in project.responsibilities" :key="i">{{ resp }}</li>
              </ul>
            </div>
            <div class="project-tech" v-if="project.technologies">
              <span v-for="(tech, i) in project.technologies" :key="i" class="tech-tag">{{ tech }}</span>
            </div>
          </div>
        </div>
      </section>
    </main>
    
    <!-- 页脚 -->
    <footer class="resume-footer">
      <p>© {{ currentYear }} {{ profile.name }} · 最后更新于 {{ lastUpdated }}</p>
    </footer>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()

// 导航功能
const goToHome = () => {
  router.push('/')
}

// 下载简历
const downloadResume = () => {
  // 这里应该是你的简历PDF文件路径
  const resumeUrl = '/resume.pdf'
  const link = document.createElement('a')
  link.href = resumeUrl
  link.download = `${profile.value.name}_个人简历.pdf`
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
}

// // 计算工作年限
// const calculateWorkYears = (startDate) => {
//   const start = new Date(startDate)
//   const now = new Date()
//   const years = now.getFullYear() - start.getFullYear()
//   const months = now.getMonth() - start.getMonth()
  
//   let totalMonths = years * 12 + months
//   if (now.getDate() < start.getDate()) {
//     totalMonths--
//   }
  
//   const fullYears = Math.floor(totalMonths / 12)
//   const remainingMonths = totalMonths % 12
  
//   if (fullYears === 0) {
//     return `${remainingMonths}个月`
//   } else if (remainingMonths === 0) {
//     return `${fullYears}年`
//   } else {
//     return `${fullYears}年${remainingMonths}个月`
//   }
// }

// 个人信息
const profile = ref({
  name: '张三',
  title: '高级前端开发工程师',
  avatar: 'https://images.unsplash.com/photo-1535713875002-d1d0cf377fde?w=200',
  contacts: [
    { icon: 'fas fa-phone', value: '138-0013-8000' },
    { icon: 'fas fa-envelope', value: 'zhangsan@example.com' },
    { icon: 'fas fa-map-marker-alt', value: '北京市海淀区' },
    { icon: 'fab fa-github', value: 'github.com/zhangsan' },
    { icon: 'fab fa-linkedin', value: 'linkedin.com/in/zhangsan' }
  ]
})

// 工作经历
const experiences = ref([
  {
    company: '某知名互联网公司',
    position: '高级前端开发工程师',
    startDate: '2020.05',
    endDate: null,
    years: calculateWorkYears('2020-05-01'),
    description: '负责公司核心产品的前端架构设计和开发工作，带领3人前端团队，优化前端性能提升30%',
    technologies: ['Vue3', 'TypeScript', '微前端', 'Webpack']
  },
  {
    company: '某科技公司',
    position: '前端开发工程师',
    startDate: '2018.03',
    endDate: '2020.04',
    years: calculateWorkYears('2018-03-01', '2020-04-30'),
    description: '参与公司SaaS平台的前端开发，独立负责用户中心模块，实现响应式设计和性能优化',
    technologies: ['React', 'Redux', 'Ant Design']
  },
  {
    company: '某软件公司',
    position: '初级前端开发工程师',
    startDate: '2016.07',
    endDate: '2018.02',
    years: calculateWorkYears('2016-07-15', '2018-02-28'),
    description: '参与多个企业级后台管理系统开发，学习并应用主流前端技术栈',
    technologies: ['jQuery', 'Bootstrap', 'Vue']
  }
])

// 教育背景
const education = ref([
  {
    school: '某某大学',
    degree: '本科',
    major: '计算机科学与技术',
    startDate: '2012.09',
    endDate: '2016.06',
    description: '主修课程：数据结构、算法、计算机网络、数据库原理、Web开发'
  }
])

// 技能专长
const skills = ref([
  {
    category: '前端技术',
    items: [
      { name: 'HTML/CSS', level: 95 },
      { name: 'JavaScript', level: 90 },
      { name: 'Vue.js', level: 88 },
      { name: 'React', level: 80 },
      { name: 'TypeScript', level: 85 }
    ]
  },
  {
    category: '构建工具',
    items: [
      { name: 'Webpack', level: 85 },
      { name: 'Vite', level: 80 },
      { name: 'Rollup', level: 70 }
    ]
  },
  {
    category: '其他技能',
    items: [
      { name: 'Node.js', level: 75 },
      { name: 'Git', level: 90 },
      { name: 'UI设计', level: 65 }
    ]
  }
])

// 项目经验
const projects = ref([
  {
    name: '企业级电商后台管理系统',
    role: '前端负责人',
    duration: '2021.03 - 2022.06',
    description: '该系统为大型电商平台的后台管理系统，包含商品管理、订单管理、用户管理、数据分析等模块',
    responsibilities: [
      '负责前端架构设计和核心模块开发',
      '实现基于Vue3的微前端架构',
      '优化打包流程，构建时间减少40%',
      '带领3人前端团队完成项目'
    ],
    technologies: ['Vue3', 'TypeScript', 'Micro Frontend', 'Element Plus']
  },
  {
    name: '移动端H5应用',
    role: '核心开发者',
    duration: '2019.08 - 2020.12',
    description: '面向消费者的移动端购物应用，支持商品浏览、下单、支付等功能',
    responsibilities: [
      '负责核心页面和组件的开发',
      '实现页面性能优化，首屏加载时间减少35%',
      '与后端协作设计API接口规范'
    ],
    technologies: ['Vue', 'Vant', 'Webpack']
  }
])

// 页脚信息
const currentYear = computed(() => new Date().getFullYear())
const lastUpdated = computed(() => {
  return new Date().toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit'
  })
})

// 计算工作年限（带结束日期参数的重载函数）
function calculateWorkYears(startDate, endDate = null) {
  const start = new Date(startDate)
  const end = endDate ? new Date(endDate) : new Date()
  
  const years = end.getFullYear() - start.getFullYear()
  const months = end.getMonth() - start.getMonth()
  
  let totalMonths = years * 12 + months
  if (end.getDate() < start.getDate()) {
    totalMonths--
  }
  
  const fullYears = Math.floor(totalMonths / 12)
  const remainingMonths = totalMonths % 12
  
  if (fullYears === 0) {
    return `${remainingMonths}个月`
  } else if (remainingMonths === 0) {
    return `${fullYears}年`
  } else {
    return `${fullYears}年${remainingMonths}个月`
  }
}

// 初始化工作年限
onMounted(() => {
  experiences.value = experiences.value.map(exp => {
    return {
      ...exp,
      years: calculateWorkYears(
        exp.startDate.replace('.', '-') + '-01',
        exp.endDate ? exp.endDate.replace('.', '-') + '-01' : null
      )
    }
  })
})
</script>

<style scoped>
.resume-container {
  --primary-color: #2c3e50;
  --secondary-color: #34495e;
  --accent-color: #3498db;
  --light-color: #ecf0f1;
  --dark-color: #2c3e50;
  --text-color: #2c3e50;
  --text-light: #7f8c8d;
  --border-color: #ddd;
  --shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  --transition: all 0.3s ease;
  
  min-height: 100vh;
  background-color: #f5f7fa;
  color: var(--text-color);
  font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
  line-height: 1.6;
  display: flex;
  flex-direction: column;
}

.resume-header {
  background-color: var(--primary-color);
  color: white;
  padding: 20px 0;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: var(--shadow);
}

.header-content {
  max-width: 1000px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.home-icon {
    display: flex;
    align-items: center;
    cursor: pointer;
    margin-right: 20px;
    transition: var(--transition);
}

.home-icon i {
    font-size: 1.8rem;
    color: white;
    background: rgba(255, 255, 255, 0.2);
    width: 50px;
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    transition: var(--transition);
}

.home-icon:hover i {
    background: rgba(255, 255, 255, 0.3);
    transform: scale(1.05);
}

.resume-title {
  margin: 0;
  font-weight: 500;
  font-size: 1.5rem;
}

.resume-main {
  flex: 1;
  max-width: 1000px;
  width: 100%;
  margin: 30px auto;
  padding: 0 20px;
  display: flex;
  flex-direction: column;
  gap: 30px;
}

/* 个人信息部分 */
.profile-section {
  display: flex;
  gap: 30px;
  background: white;
  border-radius: 8px;
  padding: 30px;
  box-shadow: var(--shadow);
}

.profile-image {
  flex-shrink: 0;
}

.avatar {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  object-fit: cover;
  border: 3px solid var(--accent-color);
}

.profile-info {
  flex: 1;
}

.name {
  margin: 0 0 5px 0;
  font-size: 1.8rem;
  color: var(--primary-color);
}

.title {
  margin: 0 0 15px 0;
  font-size: 1.2rem;
  color: var(--accent-color);
  font-weight: 500;
}

.contact-info {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 10px;
}

.contact-item {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.95rem;
}

.contact-item i {
  width: 20px;
  text-align: center;
  color: var(--accent-color);
}

/* 通用部分样式 */
.section {
  background: white;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: var(--shadow);
}

.section-title {
  margin: 0;
  padding: 15px 20px;
  background: var(--secondary-color);
  color: white;
  font-size: 1.2rem;
  display: flex;
  align-items: center;
  gap: 10px;
}

.section-title i {
  font-size: 1rem;
}

.section-content {
  padding: 20px;
}

/* 工作经历和教育背景 */
.experience-item, .education-item {
  display: flex;
  gap: 15px;
  padding: 15px 0;
}

.experience-item:not(:last-child), .education-item:not(:last-child) {
  border-bottom: 1px solid var(--border-color);
}

.timeline {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 5px;
}

.timeline-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--accent-color);
  border: 2px solid white;
  box-shadow: 0 0 0 2px var(--accent-color);
  z-index: 1;
}

.timeline-line {
  flex: 1;
  width: 2px;
  background: var(--border-color);
  margin: 5px 0;
}

.experience-content, .education-content {
  flex: 1;
}

.company, .school {
  margin: 0 0 5px 0;
  font-size: 1.1rem;
  color: var(--primary-color);
}

.meta {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin: 0 0 10px 0;
  font-size: 0.9rem;
  color: var(--text-light);
}

.position, .degree {
  font-weight: 500;
  color: var(--text-color);
}

.description {
  margin: 0;
  color: var(--text-color);
}

.description >>> p {
  margin: 0;
}

.tech-stack {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}

.tech-tag {
  background: #e0f2fe;
  color: #0369a1;
  padding: 3px 10px;
  border-radius: 50px;
  font-size: 0.8rem;
}

/* 技能专长 */
.skills {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
}

.skill-item {
  margin-bottom: 15px;
}

.skill-category {
  margin: 0 0 15px 0;
  font-size: 1.1rem;
  color: var(--primary-color);
  padding-bottom: 5px;
  border-bottom: 1px solid var(--border-color);
}

.skill {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.skill-name {
  width: 100px;
  font-size: 0.9rem;
}

.skill-bar {
  flex: 1;
  height: 8px;
  background: #eee;
  border-radius: 4px;
  overflow: hidden;
  margin: 0 10px;
}

.skill-level {
  height: 100%;
  background: var(--accent-color);
  border-radius: 4px;
  transition: var(--transition);
}

.skill-percent {
  width: 40px;
  font-size: 0.8rem;
  color: var(--text-light);
  text-align: right;
}

/* 项目经验 */
.project-item {
  padding: 15px 0;
}

.project-item:not(:last-child) {
  border-bottom: 1px solid var(--border-color);
}

.project-title {
  margin: 0 0 5px 0;
  font-size: 1.1rem;
  color: var(--primary-color);
}

.project-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin: 0 0 10px 0;
  font-size: 0.9rem;
  color: var(--text-light);
}

.role {
  font-weight: 500;
  color: var(--text-color);
}

.project-description {
  margin: 0 0 10px 0;
  color: var(--text-color);
}

.project-responsibilities {
  margin: 10px 0;
}

.project-responsibilities h4 {
  margin: 0 0 5px 0;
  font-size: 0.95rem;
  color: var(--text-color);
}

.project-responsibilities ul {
  margin: 0;
  padding-left: 20px;
}

.project-responsibilities li {
  margin-bottom: 5px;
  font-size: 0.9rem;
}

.project-tech {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}

/* 页脚 */
.resume-footer {
  text-align: center;
  padding: 20px;
  color: var(--text-light);
  font-size: 0.9rem;
  background: white;
  border-top: 1px solid var(--border-color);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .profile-section {
    flex-direction: column;
    align-items: center;
    text-align: center;
  }
  
  .contact-info {
    grid-template-columns: 1fr;
    justify-items: center;
  }
  
  .header-content {
    flex-direction: column;
    gap: 15px;
  }
  
  .resume-title {
    order: -1;
  }
  
  .skills {
    grid-template-columns: 1fr;
  }
  
  .home-icon, .download-btn {
    width: 100%;
    justify-content: center;
  }
}

@media (max-width: 480px) {
  .meta {
    flex-direction: column;
    gap: 5px;
  }
  
  .experience-item, .education-item {
    flex-direction: column;
  }
  
  .timeline {
    flex-direction: row;
    padding-top: 0;
    padding-left: 5px;
  }
  
  .timeline-dot {
    margin-right: 10px;
  }
  
  .timeline-line {
    width: 100%;
    height: 2px;
    margin: 0 5px;
  }
}
</style>